﻿using Newtonsoft.Json.Linq;
using System.Text.Json;
using Traceability.Commont.Domains;

namespace Butcher.winform
{
    public partial class SearchClient : Form
    {
        private WebBrowser webBrowser = new();
        public SearchClient()
        {
            InitializeComponent();
            webBrowser.Dock = DockStyle.Fill;
            this.Controls.Add(webBrowser);

        }
        /// <summary>
        /// 显示列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SearchClient_Load(object sender, EventArgs e)
        {
            string url = @"http://localhost:5229/api/Enterprise/SearchClients";
            HttpClient client = new HttpClient();
            Clients ctr = new Clients();
            var sh = 0;
            var content = new StringContent(JsonSerializer.Serialize<Clients>(ctr));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["date"].Value<JArray>();
            List<Clients> cts = aaa.ToObject<List<Clients>>();
            var c = 1;
            foreach (var ct in cts)
            {
                ShowClient.DataSource = cts;
            }
            if (saaa != null)
            {
                sh = 1;
            }
            else
            {
                sh = 0;
            }
        }
        private void ShowClient_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, this.ShowClient.RowHeadersWidth - 4, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), this.ShowClient.RowHeadersDefaultCellStyle.Font, rectangle, this.ShowClient.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
        private void ShowClient_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                if (this.ShowClient.Columns[e.ColumnIndex].HeaderText == "操作")
                {
                    StringFormat sf = StringFormat.GenericDefault.Clone() as StringFormat;//设置重绘入单元格的字体样式
                    sf.FormatFlags = StringFormatFlags.DisplayFormatControl;
                    sf.Alignment = StringAlignment.Center;
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Trimming = StringTrimming.EllipsisCharacter;

                    e.PaintBackground(e.CellBounds, false);//重绘边框

                    //设置要写入字体的大小
                    System.Drawing.Font myFont = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    SizeF sizeDel = e.Graphics.MeasureString("删除", myFont);
                    SizeF sizeMod = e.Graphics.MeasureString("修改", myFont);

                    float fDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width); //
                    float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width);

                    //设置每个“按钮的边界”
                    RectangleF rectDel = new RectangleF(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width * fDel, e.CellBounds.Height);
                    RectangleF rectMod = new RectangleF(rectDel.Right, e.CellBounds.Top, e.CellBounds.Width * fMod, e.CellBounds.Height);
                    e.Graphics.DrawString("删除", myFont, Brushes.Black, rectDel, sf); //绘制“按钮”
                    e.Graphics.DrawString("修改", myFont, Brushes.Black, rectMod, sf);
                    e.Handled = true;
                }
            }
        }
        private void ShowClient_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                Point curPosition = e.Location;//当前鼠标在当前单元格中的坐标
                if (this.ShowClient.Columns[e.ColumnIndex].HeaderText == "操作")
                {
                    Graphics g = this.ShowClient.CreateGraphics();
                    System.Drawing.Font myFont = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    SizeF sizeDel = g.MeasureString("删除", myFont);
                    SizeF sizeMod = g.MeasureString("修改", myFont);
                    float fDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width);
                    float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width);

                    Rectangle rectTotal = new Rectangle(0, 0, this.ShowClient.Columns[e.ColumnIndex].Width, this.ShowClient.Rows[e.RowIndex].Height);
                    RectangleF rectDel = new RectangleF(rectTotal.Left, rectTotal.Top, rectTotal.Width * fDel, rectTotal.Height);
                    RectangleF rectMod = new RectangleF(rectDel.Right, rectTotal.Top, rectTotal.Width * fMod, rectTotal.Height);
                    //判断当前鼠标在哪个“按钮”范围内
                    if (rectDel.Contains(curPosition))//删除
                    {
                        //逻辑删除
                        //将DataGridViewButton设置为按钮可删除
                        if (ShowClient.Columns[e.ColumnIndex].Name == "BtnDel" && e.RowIndex >= 0)
                        {
                            //删除前友好提示
                            if (MessageBox.Show("您确定要删除吗?", "此删除为逻辑删除", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
                            {
                                string url = @"http://localhost:5229/api/Enterprise/DeleteClients";
                                HttpClient client = new HttpClient();
                                Clients ct = new Clients();

                                // 获取被点击的行
                                DataGridViewRow row = ShowClient.Rows[e.RowIndex];

                                var k = row.Cells["ClientIDs"].Value;
                                ct.ClientID = Convert.ToInt32(k);
                                var content = new StringContent(JsonSerializer.Serialize<Clients>(ct));
                                content.Headers.Remove("Content-Type");
                                content.Headers.Add("Content-Type", "application/json");
                                string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
                                JObject info = JObject.Parse(result);
                                var saaa = result;
                                saaa = info["messges"].Value<string>();
                                if (saaa != null)
                                {

                                    MessageBox.Show("删除成功");
                                    webBrowser.Refresh();
                                }
                                else
                                {

                                    MessageBox.Show("删除失败");
                                }
                                //MessageBox.Show("删除");
                            }
                            else
                            {
                                return;
                            }
                        }
                    }
                    else if (rectMod.Contains(curPosition))//修改
                    {
                        MessageBox.Show("点击修改按钮");
                    }
                }
            }
        }
    }
}
